package cn.dnui_yxk801.dnuifood_16110100801.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.bean.FoodBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.ShopBean;
import cn.dnui_yxk801.dnuifood_16110100801.controller.FoodInfoActivity;
import cn.dnui_yxk801.dnuifood_16110100801.controller.FoodListActivity;

public class FoodAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private String shopname;
    private List<FoodBean> datalist;
    private LayoutInflater layoutInflater;
    private static String Baseurl="http://172.24.10.175:8080/foodService/";


    public FoodAdapter(Context context,String shopname ,List datalist)
    {
        this.context=context;
        this.shopname=shopname;
        this.datalist=datalist;
        layoutInflater=LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=layoutInflater.from(viewGroup.getContext()).inflate(R.layout.fooditem,viewGroup,false);
        return new FoodAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int i) {
        FoodBean entity=(FoodBean)datalist.get(i);
        if (entity==null)
            return;
        FoodAdapter.ViewHolder viewHolder=(FoodAdapter.ViewHolder)holder;
        viewHolder.foodname.setText(entity.getFoodname());
        viewHolder.price.setText(entity.getPrice());
        Picasso.with(context).load(Baseurl+entity.getPic()).into(viewHolder.food_pic);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(context,"",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent( context,FoodInfoActivity.class);
                String shop_id=datalist.get(i).getShop_id();
                String food_id=datalist.get(i).getFood_id();
                intent.putExtra("shop_id",shop_id);
                intent.putExtra("food_id",food_id);
                intent.putExtra("shop_name",shopname);
                context.startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView foodname,price;
        ImageView food_pic;


        public ViewHolder(View view) {
            super(view);
            foodname=(TextView)view.findViewById(R.id.foodname);
            price=(TextView)view.findViewById(R.id.price);
            food_pic=(ImageView)view.findViewById(R.id.foodimage);

        }
    }
}
